package com.sciencep.pro.admin.system.controller;

import com.sciencep.pro.common.domain.ResponseResult;
import com.sciencep.pro.core.domain.dto.role.RoleTO;
import com.sciencep.pro.core.domain.entity.Role;
import com.sciencep.pro.core.service.IPowerService;
import com.sciencep.pro.core.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;


@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPowerService powerService;

    @GetMapping(value = "/list")
    @ApiOperation(value = "角色管理",notes = "角色列表")
    public ResponseResult<List<RoleTO>> getRoleList(Role role){
        List<RoleTO> resultList = roleService.getRoleList(role);
        return new ResponseResult<List<RoleTO>>().success(resultList);
    }

    @GetMapping(value = "/getRoleGroupList")
    @ApiOperation(value = "角色管理" ,notes = "角色组下拉表")
    public ResponseResult<List<Role>> getRoleGroupList(){
        List<Role> resultList = roleService.getRoleGroupList();
        resultList = resultList.stream().collect
                (Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Role :: getGroupId))), ArrayList::new));
        return new ResponseResult<List<Role>>().success(resultList);
    }

    @GetMapping(value = "/getRoleNameByGroupId")
    @ApiOperation(value = "角色管理",notes = "下拉框(组名联动)")
    public ResponseResult<List<Role>> getRoleNameByGroupId(Role role){
        List<Role> resultList = roleService.getRoleNameByGroupId(role);
        return new ResponseResult<List<Role>>().success(resultList);
    }

}
